<template>
  <div class="table-page">
    <GiTable title="测试任务用户关系管理" row-key="id" :data="dataList" :columns="columns" :loading="loading"
      :scroll="{ x: '100%', y: '100%', minWidth: 1000 }" :pagination="pagination" :disabled-tools="['size']"
      :disabled-column-keys="['name']" @refresh="search">
      <template #toolbar-left>
        <a-input v-model="queryForm.taskId" placeholder="请输入测试任务ID" allow-clear @change="search">
          <template #prefix><icon-search /></template>
        </a-input>
        <a-input v-model="queryForm.userId" placeholder="请输入用户ID" allow-clear @change="search">
          <template #prefix><icon-search /></template>
        </a-input>
        <a-button @click="reset">
          <template #icon><icon-refresh /></template>
          <template #default>重置</template>
        </a-button>
      </template>
      <template #toolbar-right>
        <a-button v-permission="['task:userParticipant:add']" type="primary" @click="onAdd">
          <template #icon><icon-plus /></template>
          <template #default>新增</template>
        </a-button>
        <a-button v-permission="['task:userParticipant:export']" @click="onExport">
          <template #icon><icon-download /></template>
          <template #default>导出</template>
        </a-button>
      </template>
      <template #action="{ record }">
        <a-space>
          <a-link v-permission="['task:userParticipant:detail']" title="详情" @click="onDetail(record)">详情</a-link>
          <a-link v-permission="['task:userParticipant:update']" title="修改" @click="onUpdate(record)">修改</a-link>
          <a-link v-permission="['task:userParticipant:delete']" status="danger" :disabled="record.disabled"
            :title="record.disabled ? '不可删除' : '删除'" @click="onDelete(record)">
            删除
          </a-link>
        </a-space>
      </template>
    </GiTable>

    <UserParticipantAddModal ref="UserParticipantAddModalRef" @save-success="search" />
    <UserParticipantDetailDrawer ref="UserParticipantDetailDrawerRef" />
  </div>
</template>

<script setup lang="ts">
import UserParticipantAddModal from './UserParticipantAddModal.vue'
import UserParticipantDetailDrawer from './UserParticipantDetailDrawer.vue'
import { useDownload, useTable } from '@/hooks'
import { useDict } from '@/hooks/app'
import { isMobile } from '@/utils'
import has from '@/utils/has'
import {
  type UserParticipantResp,
  type UserParticipantQuery,
  deleteUserParticipant,
  exportUserParticipant,
  listUserParticipant
} from '@/apis/task/userParticipant'
import type { TableInstanceColumns } from '@/components/GiTable/type'

defineOptions({ name: 'UserParticipant' })


const queryForm = reactive<UserParticipantQuery>({
  taskId: undefined,
  userId: undefined,
  sort: ['id,desc']
})

const {
  tableData: dataList,
  loading,
  pagination,
  search,
  handleDelete
} = useTable((page) => listUserParticipant({ ...queryForm, ...page }), { immediate: true })
const columns: TableInstanceColumns[] = [
  { title: '测试任务ID', dataIndex: 'taskId', slotName: 'taskId' },
  { title: '用户ID', dataIndex: 'userId', slotName: 'userId' },
  {
    title: '操作',
    dataIndex: 'action',
    slotName: 'action',
    width: 160,
    align: 'center',
    fixed: !isMobile() ? 'right' : undefined,
    show: has.hasPermOr(['task:userParticipant:detail', 'task:userParticipant:update', 'task:userParticipant:delete'])
  }
]

// 重置
const reset = () => {
  queryForm.taskId = undefined
  queryForm.userId = undefined
  search()
}

// 删除
const onDelete = (record: UserParticipantResp) => {
  return handleDelete(() => deleteUserParticipant(record.id), {
    content: `是否确定删除该条数据？`,
    showModal: true
  })
}

// 导出
const onExport = () => {
  useDownload(() => exportUserParticipant(queryForm))
}

const UserParticipantAddModalRef = ref<InstanceType<typeof UserParticipantAddModal>>()
// 新增
const onAdd = () => {
  UserParticipantAddModalRef.value?.onAdd()
}

// 修改
const onUpdate = (record: UserParticipantResp) => {
  UserParticipantAddModalRef.value?.onUpdate(record.id)
}

const UserParticipantDetailDrawerRef = ref<InstanceType<typeof UserParticipantDetailDrawer>>()
// 详情
const onDetail = (record: UserParticipantResp) => {
  UserParticipantDetailDrawerRef.value?.onOpen(record.id)
}
</script>

<style scoped lang="scss"></style>
